<?php

declare(strict_types=1);

namespace xpzhu\Jwt;

use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Hmac;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\LocalFileReference;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa;
use xpzhu\Jwt\Exception\JwtException;

class Config
{
    protected bool       $sso_enable        = false;
    protected            $signer_key        = null;
    protected int        $not_before        = 0;
    protected int        $expires_at        = 3600;
    protected int        $refresh_ttl       = 7200;
    protected string     $signer            = \Lcobucci\JWT\Signer\Hmac\Sha256::class;
    protected string     $type              = 'Header';
    protected string     $iss               = 'client.jwt.com';
    protected string     $aud               = 'server.jwt.com';
    protected bool       $automatic_renewal = false;
    protected string     $public_key        = '';
    protected string     $private_key       = '';

    /**
     * Config constructor.
     * @param array $options
     */
    public function __construct(array $options)
    {
        foreach ($options as $key => $value) {
            $this->$key = $value;
        }
    }

    /**
     * 获取加密密钥
     * @return Key
     */
    public function getHmacKey(): Key
    {
        if (empty($this->signer_key)) {
            throw new JwtException('config signer_key required', 500);
        }

        return InMemory::base64Encoded((string)$this->signer_key);
    }

    /**
     * 是否使用 RSA 加密
     * @return bool
     */
    public function RSASigner()
    {
        $signer = $this->getSigner();
        return $signer instanceof Rsa;
    }

    /**
     * 获取加密key
     * @return Key
     */
    public function getSignerKey(): Key
    {
        $signer = $this->getSigner();

        if ($this->RSASigner()) {
            return $this->getPrivateKey();
        } else if ($signer instanceof Hmac) {
            return $this->getHmacKey();
        } else {
            throw new JwtException('not support', 500);
        }
    }

    /**
     * 获取 RSA 公钥
     * @return Key
     */
    public function getPublicKey(): Key
    {
        return LocalFileReference::file($this->public_key);
    }

    /**
     * 获取 RSA 私钥
     * @return Key
     */
    public function getPrivateKey(): Key
    {
        return LocalFileReference::file($this->private_key);
    }

    /**
     * 获取有效果期
     * @return int
     */
    public function getExpires()
    {
        return $this->expires_at;
    }

    /**
     * get refresh ttl
     * @return int
     */
    public function getRefreshTtl()
    {
        return $this->refresh_ttl;
    }

    /**
     * get subject
     * @return string
     */
    public function getSubject()
    {
        return md5(uniqid() . time() . rand(100000, 9999999));
    }

    /**
     * get iss
     * @return string
     */
    public function getIss(): string
    {
        return $this->iss;
    }

    /**
     * get aud
     * @return string
     */
    public function getAud(): string
    {
        return $this->aud;
    }

    /**
     * get not before
     * @return int
     */
    public function getNotBefore()
    {
        return $this->not_before;
    }

    /**
     * get signer
     * @return Signer
     */
    public function getSigner(): Signer
    {
        return new $this->signer;
    }

    /**
     * get automatic renewal
     * @return bool
     */
    public function getAutomaticRenewal()
    {
        return $this->automatic_renewal;
    }

    /**
     * get type
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * get sso_enable
     * @return string
     */
    public function getSsoEnable()
    {
        return $this->sso_enable;
    }
}
